package cn.edu.nchu.comic_strip.ui.bookmark;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.List;

import cn.edu.nchu.comic_strip.R;
import cn.edu.nchu.comic_strip.model.BookMarkBook;

public class BookMarkBookListAdapter extends ArrayAdapter<BookMarkBook> {
    private Context mContext;

    public BookMarkBookListAdapter(Context context, List<BookMarkBook> objects) {
        super(context, 0, objects);
        this.mContext = context;  // 初始化 mContext
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.book_mark_book_item, parent, false);
        }

        BookMarkBook bookMarkBook = getItem(position);

        if (bookMarkBook != null) {
            TextView bookName = convertView.findViewById(R.id.book_name);
            TextView bookMarkCount = convertView.findViewById(R.id.book_mark_book_count);
            ImageView bookCover = convertView.findViewById(R.id.book_cover);

            bookName.setText(bookMarkBook.getBookName());
            bookMarkCount.setText(String.valueOf(bookMarkBook.getCount_book_mark()));

            // 解析资源名称字符串，获取对应的资源 ID
            int resourceId = mContext.getResources().getIdentifier(bookMarkBook.getBookCover(), "drawable", mContext.getPackageName());
            bookCover.setImageResource(resourceId);
        }

        return convertView;
    }
}
